<template>
	<view style="background-color:#181816 ;height: 100%;">
		<view class="big_logo">
			<image src="../../static/big_logo.png"></image>
		</view>
		
		<view class="login_btn">
			<button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"  >
				微信授权登录
			</button>
			
		</view>
		<view class="agreement">
			
		</view>
	</view>
</template>

<script>
	import pop from "@/components/ming-pop/ming-pop.vue";
	import api from '@/request/api.js'
	import wxlogin from '@/common/wxlogin.js';
	export default {
		data() {
			return {
				useInfo: wx.getStorageSync('useInfo') || ''
			}
		},
		methods: {
			// 授权登录
			getUserInfo() {
				wxlogin.wechatAppLogin()
			},
			// 手机号授权
			onGetPhoneNumber(e) {
				console.log(e)
				wxlogin.getPhoneNumber(e)
			},
			/* 以前登录方法 */
			login() {
				// 获取用户信息
				uni.login({
					success: res => {
						// data.code作为参数传给给后台
						console.log(res)
						api.getSessionKey({
							'js_code': res.code
						}).then(res => {
							console.log('登录信息接口返回:', res)
							getApp().globalData.session_key = res.data.session_key
							getApp().globalData.openid = res.data.openid
							console.log(getApp().globalData.session_key)
						}).catch(err => {
							console.log(err)
						})

						// 此时传入加密的code码，后端会返回用户unionid、openid等信息
						/* this.getOpenId({ code: res.code }).then(res => {
					                uni.setStorageSync('unionid', res.data.unionid) //存入缓存待用
					                resolve(res.data.openid)
					            }).catch(err => {
					            	reject(err)
					            }) */
					},
					fail: err => {
						console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err)
						reject(err)
					}
				})
				/* 调用起微信一键登录 */
				uni.getUserProfile({
					desc: '获取你的昵称、头像、地区及性别',
					success: res => {
						console.log(res);

						/* 获取成功给后台返回用户信息 */
						api.setUserInfo({
							'avatarUrl': res.userInfo.avatarUrl,
							'gender': res.userInfo.gender,
							'nickName': res.userInfo.nickName,
							'openid': getApp().globalData.openid
						}).then(res => {
							console.log(res)
							/* 存全局变量 */
							getApp().globalData.nickName = res.data.nickname
							getApp().globalData.avatarUrl = res.data.avatar
							getApp().globalData.is_auth = res.data.is_auth
							getApp().globalData.token = res.data.token
							getApp().globalData.uid = res.data.id
							getApp().globalData.nft_address = res.data.nft_address
							console.log(res.data.is_auth)
							//将用户信息存入本地缓存
							uni.setStorage({
								key: 'userInfo',
								data: {
									'openid': getApp().globalData.openid,
									'nickName': res.data.nickname,
									'avatarUrl': res.data.avatar,
									'token': res.data.token,
									'is_auth': res.data.is_auth,
									'uid': res.data.id,
									'mobile': res.data.mobile,
									'update_time': res.data.update_time,
									'nft_address': res.data.nft_address
								}
							});
							/* 判断用户是否实名认证 */

						}).catch(err => {
							/* console.log(err) */

						})
						this.head = res.userInfo.avatarUrl
						this.username = res.userInfo.nickName
						/* this.hasLogin = true */
						getApp().globalData.hasLogin = true
						this.hasLogin = getApp().globalData.hasLogin
						/* console.log(this.username)
						console.log(this.hasLogin)
						console.log(getApp().globalData.hasLogin)    */
						// 设置方法强制加载页面，若实名认证过的用户实名认证会消失
						setTimeout(() => {
							uni.reLaunch({
								url: 'mine'
							})
						}, 500)
					},
					fail: res => {
						console.log("error")
						console.log(res)
						//拒绝授权
						return;
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
	
		background-color: #181816;
	}

	.big_logo {
		height: 300rpx;
		width: 300rpx;
		margin: 200rpx auto 0 auto;

		image {
			height: 100%;
			width: 100%;
		}
	}

	.login_title {
		background-image: linear-gradient(-90deg, #f0c611 6%, #4effff 100%);
		background-clip: text;
		margin-top: 45rpx;
		text-align: center;
		letter-spacing: 3rpx;
		font-size: 45rpx;
		font-weight: bold;
		-webkit-background-clip: text;
		color: transparent;
	}

	.login_btn {
		width: 436rpx;
		height: 90rpx;
		line-height: 90rpx;
		margin: 200rpx auto 0 auto;
		background-color: #09bb07;
		border-radius: 45rpx;

		button {
			width: 100%;
			height: 100%;
			line-height: 90rpx;
			margin: 200rpx auto 0 auto;
			color: #fff;
			background-color: #09bb07;
			border-radius: 45rpx;
		}
	}
</style>
